package dfsketches.pathfind;

public class BitMap {
	{
		masks = new int[32];
		int mask = 0x80000000;
		for (int i = 0; i < 32; i++) {
			masks[i] = mask;
			mask >>>= 1;
		}		
	}
	
	public BitMap(int[][] walls) {
		h = walls.length;
		w = walls[0].length;
		this.walls = new int[h][(w + 31) >>> 5];
		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				if (walls[y][x] != 0) {
					this.walls[y][x >>> 5] |= masks[x & mask];
				}
			}
		}
	}

	public boolean isWall(int x, int y) {
		return (walls[y][x >>> 5] & masks[x & mask]) != 0;
	}
	
	public void addWall(int x, int y) {
		walls[y][x >>> 5] |= masks[x & mask];
	}
	
	public void removeWall(int x, int y) {
		walls[y][x >>> 5] &= ~masks[x & mask];
	}
	
	public int width() {
		return w;
	}
	
	public int height() {
		return h;
	}
	
	private int w;
	private int h;
	private int[][] walls;
	
	private static int[] masks;
	private static final int mask = 0x177;	//last five bits

}